<?php
// +----------------------------------------------------------------------+
// | The Answern Inc                                                  |
// +----------------------------------------------------------------------+
// | Copyright (c) 2017, Answern Inc. All rights reserved.            |
// +----------------------------------------------------------------------+
// | Authors: Answern Inc. 
// |                                                                      |
// +----------------------------------------------------------------------+
 
/**
 * @version  1.0
 * @author   zxiaofee
 * @date     2017-05-11
 */

namespace App\Controllers\Common;
use \App\Libs\Base\Constant;
use App\Models\User\User;

/**
*	设置用户信息
*/
class SetUserINFO extends \App\Libs\Base\BaseAction
{
	 protected function beforeRun() {
         parent::beforeRun();
         $this->needLogin = TRUE;
     }
	public function execute() {
		$user = $this->session->user;
		$avatar = $this->request->get('avatar');//头像
		$username = $this->request->get('username');//昵称
		$babyName = $this->request->get('babyName');
		$babySex = empty( $this->request->get('babySex') )?0:$this->request->get('babySex');
		$babyBirthday = $this->request->get('babyBirthday');
		if( empty($avatar) ){
			return \App\Libs\Views\Json::view(1, '请上传头像', null);
		}
		if( empty( $username ) ){
			return \App\Libs\Views\Json::view(1, '请添加昵称', null);	
		}
		
		$user->avatar = $avatar;
		$user->username = $username;
		$user->baby_name = $babyName;
		$user->baby_sex = $babySex;
		$user->baby_birthday  = $babyBirthday;
		$result = $user->save();
		if( !$result ){
			//失败
			return \App\Libs\Views\Json::view(1, '失败', null);
		}
		\App\Libs\Session\Session::resetCache($user);
		//$user->vip = $user->vip == 1?'已开通':'未开通';
		$data = array(
			'token' => $user->token,
			'name' => $user->username,
			'avatar' => $user->avatar,
			'baby_name' => $user->baby_name,
			'baby_sex' => $user->baby_sex,
			'baby_birthday' => $user->baby_birthday,
			'vip' => $user->vip == 1?'已开通':'未开通',
			'balance' => $user->balance,
			'type' => 1,
			'phone' => $user->phone,
			'age' => \App\Libs\Helper\CommonHelper::age($user->baby_birthday)
		 );
		return \App\Libs\Views\Json::view(0, '成功', $data);

		
	}
}
